#include "wuSolidRectManager.h"

wuSolidRectManager::wuSolidRectManager()
{

}

wuSolidRectManager::~wuSolidRectManager()
{
}

void wuSolidRectManager::setup(ofxBox2d * _box2d, int _vw, int _vh)
{
	wuBase2dManager::setup(_box2d, _vw, _vh);
	bSkinOn = false;
}

bool wuSolidRectManager::action(ofImage *currImg, ofImage *bgImg, ofPoint _pos, int action, int x_offset)
{

    x_scroll = x_offset*vPriority;

	if(!bSkinOn)
	{
		wuBaseManager::capture(currImg, bgImg, x_offset);
		bSkinOn = true;
	}
	else
		createNewSolidRect(_pos);

	return false;
}

void wuSolidRectManager::addBitxo(ofImage *_skin, ofRectangle rect, vector <ofPoint> * _pts )
{
	generalSkin.setFromPixels(_skin->getPixels(), rect.width, rect.height, OF_IMAGE_COLOR_ALPHA);
}

void wuSolidRectManager::update()
{
        for (int i = 0; i < boxes.size(); i++)
            boxes[i].update();
}

void wuSolidRectManager::draw(bool bDrawlines, int offX)
{
        for (int i = 0; i < boxes.size(); i++)
            boxes[i].draw(offX);
}

void wuSolidRectManager::drawSelected(bool bDrawlines, int offX)
{
    if(selection.width < vw || selection.height < vh)
    {

        for (int i = boxes.size()-1; i >= 0; i--)
        {
            bool condi = (boxes[i].getPosition().y > selection.y) && (boxes[i].getPosition().y < selection.y + selection.height) &&
             (boxes[i].getPosition().x > selection.x + x_scroll) && (boxes[i].getPosition().x < selection.x + x_scroll + selection.width);

            if (condi)
                boxes[i].draw(offX);
        }
    }else
    {
       for (int i = 0; i < boxes.size(); i++)
            boxes[i].draw(offX);
    }
}

void wuSolidRectManager::clear()
{
    if(selection.width < vw || selection.height < vh)
    {

        for (int i = boxes.size()-1; i >= 0; i--)
        {
            bool condi = (boxes[i].getPosition().y > selection.y) && (boxes[i].getPosition().y < selection.y + selection.height) &&
             (boxes[i].getPosition().x > selection.x + x_scroll) && (boxes[i].getPosition().x < selection.x + x_scroll + selection.width);

            if (condi)
                boxes.erase(boxes.begin()+i);
        }
    }else
    {
       for (int i = 0; i < boxes.size(); i++)
            boxes[i].clear();
       boxes.clear();
    }
}

void wuSolidRectManager::touch(int _x,int _y, int forceX, int forceY)
{
    for (int i = 0; i < boxes.size(); i++)
		boxes[i].touch(_x, _y, forceX, forceY);
}

bool wuSolidRectManager::drag(int _x, int _y)
{
        bool bDrag=false;

        for (int i = 0; i < boxes.size(); i++)
            if(boxes[i].drag(_x + x_scroll, _y))
                bDrag=true;

        return bDrag;
}

void wuSolidRectManager::createNewSolidRect(ofPoint _pos)
{
		float w = ofRandom(7, 30);
		float h = ofRandom(7, 30);

		wuSolidRect box;
		box.setPhysics(3.0, 0.0, 0.1);
		box.setup(box2d->getWorld(), _pos.x, _pos.y, w, h);
		box.setDim(w,h);
		box.setImg( &generalSkin );
		boxes.push_back(box);
}
